/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.ucase.casemain.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.icinfo.cloud.common.security.util.UserUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CaseProsecutionJudgmentMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseProsecutionJudgment;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseProsecutionRegister;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseProsecutionJudgmentService;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseProsecutionFileVo;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseProsecutionJudgmentVo;
import com.icinfo.cloud.xzcf.provider.member.userinfo.dto.UserDTO;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 描述: 案件诉讼判决表 case_prosecution_judgment 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年12月01日
 */
@Service
public class CaseProsecutionJudgmentServiceImpl extends MyBatisServiceSupport implements ICaseProsecutionJudgmentService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(CaseProsecutionJudgmentServiceImpl.class);

    @Resource
    private CaseProsecutionJudgmentMapper caseProsecutionJudgmentMapper;

    /**
     * 获取诉讼判决信息（根据id）
     *
     * @param id 诉讼判决唯一标识
     * @return 诉讼判决信息
     * @author lilihao
     * @date 2022/12/5 11:26
     */
    @Override
    public CaseProsecutionJudgment getInfById(String id) {
        return caseProsecutionJudgmentMapper.selectByPrimaryKey(id);
    }

    /**
     * 获取诉讼判决信息
     *
     * @param caseProsecutionJudgment 请求信息
     * @return 诉讼判决信息
     * @author lilihao
     * @date 2022/12/5 11:26
     */
    @Override
    public CaseProsecutionJudgmentVo getInf(CaseProsecutionJudgment caseProsecutionJudgment) {
        Example example = new Example(CaseProsecutionRegister.class);
        example.createCriteria().andEqualTo("caseId", caseProsecutionJudgment.getCaseId())
                .andEqualTo("documentNumber", caseProsecutionJudgment.getDocumentNumber())
                .andEqualTo("isDeleted", Constant.STRING_0)
                .andEqualTo("trialState", caseProsecutionJudgment.getTrialState());
        List<CaseProsecutionJudgment> list = caseProsecutionJudgmentMapper.selectByExample(example);
        CaseProsecutionJudgmentVo prosecutionJudgmentVo = CollectionUtil.isEmpty(list) ? null : BeanUtil.toBean(list.get(0), CaseProsecutionJudgmentVo.class);
        if (CollectionUtil.isNotEmpty(list) && StrUtil.isNotBlank(list.get(0).getLitigationFile())) {
            JSONArray objects = JSONUtil.parseArray(list.get(0).getLitigationFile());
            List<CaseProsecutionFileVo> fileList = JSONUtil.toList(objects, CaseProsecutionFileVo.class);
            prosecutionJudgmentVo.setFileList(fileList);
        }
        return prosecutionJudgmentVo;
    }

    /**
     * 保存诉讼判决信息
     *
     * @param caseProsecutionJudgment 诉讼判决信息
     * @return 诉讼判决信息
     * @author lilihao
     * @date 2022/12/5 11:26
     */
    @Override
    public void saveInfo(CaseProsecutionJudgment caseProsecutionJudgment) {
        caseProsecutionJudgment.setCreateTime(new Date());
        caseProsecutionJudgment.setIsDeleted(Constant.STRING_0);
        UserDTO userInfo = UserUtil.getUserInfo();
        caseProsecutionJudgment.setCreateUserId(ObjectUtil.isNotNull(userInfo) ? String.valueOf(userInfo.getUserId()) : "");
        caseProsecutionJudgmentMapper.insert(caseProsecutionJudgment);
    }

    /**
     * 更新诉讼判决信息
     *
     * @param caseProsecutionJudgment 诉讼判决信息
     * @return 诉讼判决信息
     * @author lilihao
     * @date 2022/12/5 11:26
     */
    @Override
    public void updateInfo(CaseProsecutionJudgment caseProsecutionJudgment) {
        caseProsecutionJudgment.setUpdateTime(new Date());
        caseProsecutionJudgment.setIsDeleted(Constant.STRING_0);
        caseProsecutionJudgment.setUpdateUserId(String.valueOf(UserUtil.getUserInfo().getUserId()));

        Example example = new Example(CaseProsecutionJudgment.class);
        example.createCriteria()
                .andEqualTo("caseId", caseProsecutionJudgment.getCaseId())
                .andEqualTo("documentNumber", caseProsecutionJudgment.getDocumentNumber())
                .andEqualTo("isDeleted", Constant.STRING_0)
                .andEqualTo("trialState", caseProsecutionJudgment.getTrialState());
        caseProsecutionJudgmentMapper.updateByExampleSelective(caseProsecutionJudgment, example);
    }
}